﻿namespace com.crownpack.com
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    using Microsoft.SharePoint.Client;

    class Program
    {
        #region Fields

        //private static string RootURL = "http://win-pv542842fga:8888/sites/cp";
        //private static string URL = "http://win-pv542842fga:8888/sites/cp/i2e/";
        private static string RootURL = "http://cpserver30:40020/I2E";

        #endregion Fields

        #region Methods

        static void GetFields( FieldCollection fields )
        {
            foreach ( Field f in fields )
            {
                Console.WriteLine( f.StaticName + " '" + f.Title + "' Guid=" + f.Id );
            }
        }

        static void GetItems( ListItemCollection items )
        {
            foreach ( ListItem item in items )
            {
                Dictionary<string, object> values = item.FieldValues;
                foreach ( KeyValuePair<string, object> kvp in values )
                {
                    Console.WriteLine( string.Format( "Key={0}  Value='{1}'", kvp.Key, kvp.Value == null ? string.Empty : kvp.Value.ToString( ) ) );
                }
            }
        }

        private static void GetListItemsBasic( string url )
        {
            using ( ClientContext ctx = new ClientContext( url ) )
            {
                Web web = ctx.Web;
                ctx.Load( web );
                ctx.Load( web.Lists );
                ctx.ExecuteQuery( );
                ListCollection lists = web.Lists;
                foreach ( List l in lists )
                {
                    ctx.Load( l.RootFolder );
                    ctx.Load( l.Fields );
                    CamlQuery query = new CamlQuery( );
                    ListItemCollection items = l.GetItems( query );
                    ctx.Load( items );
                    ctx.ExecuteQuery( );
                    Console.WriteLine( l.Title + " = " + l.RootFolder.Name + " Item Count=" + l.ItemCount );
                    Console.WriteLine( "Is Hidden=" + l.Hidden + " Item Count=" + l.ItemCount );

                    GetFields( l.Fields );
                    if ( l.RootFolder.Name.Equals( "initiatives" ) )
                        GetItems( items );
                }
                //List list = web.Lists.GetByTitle( LIST_NAME );
                //ctx.ExecuteQuery( );
                //CamlQuery query = new CamlQuery( );
                //ListItemCollection items = list.GetItems( query );
                //ctx.Load( items );

                //ctx.ExecuteQuery( );

                //Console.WriteLine( "ListItem count:{0}", items.Count );
            }
        }

        //private static string LIST_NAME = "Initiatives";
        static void GetWebs( string url )
        {
            using ( ClientContext ctx = new ClientContext( url ) )
            {
                Web web = ctx.Web;
                ctx.Load( web );
                ctx.Load( web.Webs );
                ctx.ExecuteQuery( );
                Console.WriteLine( web.Title + " " + web.Id + " " + web.ServerRelativeUrl );
                GetListItemsBasic( url );
                Uri uri = new Uri( ctx.Url );
                foreach ( Web w in web.Webs )
                {
                    Uri wuri = new UriBuilder( uri.Scheme, uri.Host, uri.Port, w.ServerRelativeUrl ).Uri;
                    GetWebs( wuri.ToString( ) );
                }
            }
        }

        //http://zimmergren.net/technical/sp-2010-getting-started-with-the-client-object-model-in-sharepoint-2010
        static void Main( string[ ] args )
        {
            try
            {
                GetWebs( RootURL );
                //GetListItemsBasic( RootURL );
            }
            catch ( Exception e )
            {
                Console.WriteLine( e.Message );
                if ( e.InnerException != null )
                    Console.WriteLine( e.InnerException.Message );
            }
            Console.ReadLine( );
        }

        #endregion Methods
    }
}